var sysObj = {}
var sysOutObj = {}
var groupObj = {}
$(function () {
  //toolbar权限控制
  var editFlag = permissions.pre_msg_edit
  var delFlag = permissions.pre_msg_del
  var copyFlag = permissions.pre_msg_copy
  var addFlag = permissions.pre_msg_add
  if (!addFlag) {
    $('#addBtn').remove()
  }
  getInsystem()
  getOutsystem()
  getAllGroup()
  searchBox('0')
  $('#commType').change(function(){
    var val = $(this).val()
    searchBox(val)
  })
  $('#sysCode').change(function(){
    var val = $(this).val()
    if (val) getGroup(val)
    $('#inSysName').val(val)
  })
  $('#thirdSys').change(function(){
    var val = $(this).val()
    $('#inSysName').val(val)
  })

  var options = {
    url: api + "/comm/syscommpkt/page",
    updateUrl: "/pages/pre/msg/edit",
    removeUrl: api + "/comm/syscommpkt/{id}",
    modalName: "报文配置",
    columns: [
      {
        title: "序号",
        formatter: function (value, row, index) {
          return $.table.serialNumber(index);
        }
      },
      {
        field: 'inSysName',
        title: '项目名称'
      },
      {
        field: 'systemCode',
        title: '项目编码'
      },
      {
        field: 'groupName',
        title: '服务名称'
      },
      {
        field: 'groupCode',
        title: '服务编码'
      },
      {
        field: 'transCode',
        title: '交易码'
      },
      {
        title: '操作',
        formatter: function (value, row, index) {
          var actions = [];
          if (editFlag) {
            var url = "/pages/pre/msg/edit?commType="+row.commType+"&systemCode="+row.systemCode+"&groupCode="+(row.groupCode?row.groupCode:'')+"&transCode="+row.transCode+"&oper=edit"
            actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="$.operate.editFullBpm(\'' + row.pktId + '\',\'\',\'' + url + '\')"><i class="fa fa-edit"></i>编辑</a> ');
          }
          if (delFlag) {
            actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.pktId + '\')"><i class="fa fa-remove"></i>删除</a> ');
          }
          if (copyFlag) {
            actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="copyMsg(\'' + row.pktId + '\')"><i class="fa fa-copy"></i>复制</a> ');
          }
          return actions.join('');
        }
      }]
  };
  $.table.init(options);
});

//判断显示搜索栏
function searchBox(val) {
  if (val == 0) {
    $('#sysCode').parents('li').show()
    $('#groupCode').parents('li').show()
    $('#thirdSys').parents('li').hide()
  }else if (val == 1){
    $('#sysCode').parents('li').hide()
    $('#groupCode').parents('li').hide()
    $('#thirdSys').parents('li').show()
  }
}

//获取接入项目列表
function getInsystem(){
  $.operate.get(api + '/comm/insystem/insys_list', function (res) {
    res.data.forEach(function (item) {
      $('#sysCode').append('<option value="' + item.code + '">' + item.name + '</option>')
      sysObj[item.sysId] = item.name
    })
  }, false)
}

//获取接出项目列表
function getOutsystem(){
  $.operate.get(api + '/comm/outsystem/outsys_list', function (res) {
    res.data.forEach(function (item) {
      $('#thirdSys').append('<option value="' + item.code + '">' + item.name + '</option>')
      sysOutObj[item.sysId] = item.name
    })
  }, false)
}
//根据接入项目名称获取服务列表
function getGroup(code){
  $('#groupCode').find('option').remove()
  $('#groupCode').append('<option value=""></option>')
  $.operate.get(api + '/comm/group/group_list/code/'+code, function (res) {
    res.data.forEach(function (item) {
      $('#groupCode').append('<option value="' + item.code + '">' + item.name + '</option>')
    })
  }, false)
}

//获取服务所有列表
function getAllGroup(){
  $.operate.get(api + '/comm/group/group_list/all', function (res) {
    res.data.forEach(function (item) {
      groupObj[item.groupId] = item.name
    })
  }, false)
}

//复制
function copyMsg(id) {
  $.modal.open("复制报文", "/pages/pre/msg/copy?id="+id);
}

function tableReset() {
  $('#sysCode').val('')
  $('#groupCode').val('')
  $('#thirdSys').val('')
  $('#transCode').val('')
  $('#inSysName').val('')
  $("#bootstrap-table").bootstrapTable('refresh');
}